package com.yh.service.impl;

import com.yh.mapper.OrderMapper;
import com.yh.mapper.ProductMapper;
import com.yh.pojo.Product;
import com.yh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author by 张晨光
 * @date 2023/11/2 14:25
 */
@Service
public class ProductServiceImpl implements ProductService {

    //调用mapper对象
    @Autowired
    ProductMapper productMapper;
    //这里增加一个对orderMapper的调用;
    @Autowired
    OrderMapper orderMapper;

    //@RolesAllowed("ADMIN")   //({"ADMIN"})  或({"ADMIN","USER"})
//    @Secured("ROLE_ADMIN")
    @PreAuthorize("authentication.principal.username == 'jack'")
    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);
    }

    @Override
    public Product findById(int pid) {
        return productMapper.findById(pid);
    }

    @Override
    public void edit(Product product) {
        productMapper.edit(product);
    }

    /**
     * 业务层的根据id，来删除某个数据，包含了两个mapper的操作；
     *     boolean findProductById
     *          如果，没有，则调用productMapper.deleteById(pid);
     * @param pid
     */
    @Transactional
    @Override
    public void deleteById(Integer pid) {
        orderMapper.updateById(pid);
        productMapper.deleteById(pid);
    }
}
